package net.sky.oa.util;

import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonBuilderUtil {

	private String[] includeFields;
	
	private String[] excludeFields;

	private Class<?>[] excludeClasses;

	private String datePattern = "yyyy-MM-dd";

	public GsonBuilderUtil setDateFormat(String datePattern) {
		this.datePattern = datePattern;
		return this;
	}

	public GsonBuilderUtil() {
	}
	
	public GsonBuilderUtil(String excludeFields) {
		this.excludeFields = excludeFields.split("[,]");
	}

	public GsonBuilderUtil(String[] excludeFields) {
		this.excludeFields = excludeFields;
	}

	public GsonBuilderUtil(Class<?>[] excludeClasses) {
		this.excludeClasses = excludeClasses;
	}

	public GsonBuilderUtil(String[] excludeFields, Class<?>[] excludeClasses) {
		this.excludeFields = excludeFields;
		this.excludeClasses = excludeClasses;
	}

	public GsonBuilderUtil setExcludeFields(String[] excludeFields) {
		this.excludeFields = excludeFields;
		return this;
	}

	public GsonBuilderUtil setExcludeClasses(Class<?>[] excludeClasses) {
		this.excludeClasses = excludeClasses;
		return this;
	}
	
	public GsonBuilderUtil setExcludeFields(String fields) {
		if(StringUtils.isNotEmpty(fields)){
			this.excludeFields = fields.split("[,]");
		}else{
			this.excludeFields = null;
		}
		return this;
	}
	
	public GsonBuilderUtil setIncludeFields(String fields) {
		if(StringUtils.isNotEmpty(fields)){
			this.includeFields = fields.split("[,]");
		}else{
			this.includeFields =null;
		}
		return this;
	}
	
	public GsonBuilderUtil setFields(String includeFields, String excludeFields) {
		setIncludeFields(includeFields);
		setExcludeFields(excludeFields);
		return this;
	}
	
	public Gson getGson() {
		return getGsonBuilder().create();
	}
	/**
	 * 当includeFields 与 excludeFields 都为空时,则不进行过滤
	 * 当仅includeFields不为空时, 非includeFields都过滤
	 * 当仅excludeFields不为空时, 非excludeFields都不过滤
	 * 当includeFields 与 excludeFields 都不为空时,则同时进行过滤
	 * @return
	 */
	public GsonBuilder getGsonBuilder() {
		GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
			public boolean shouldSkipClass(Class<?> skipClass) {
				if (null != excludeClasses && excludeClasses.length > 0) {
					for (Class filterClass : excludeClasses) {
						if (filterClass.equals(skipClass))
							return true;
					}
				}
				return false;
			}

			public boolean shouldSkipField(FieldAttributes field) {
				if (null != includeFields && includeFields.length > 0) {
					for (String fieldName : includeFields) {
						if (field.getName().equalsIgnoreCase(fieldName)){
							return false;
						}
					}
				}
				if (null != excludeFields && excludeFields.length > 0) {
					for (String fieldName : excludeFields) {
						if (field.getName().equalsIgnoreCase(fieldName)){
							return true;
						}
					}
				}
				int flag=0;
				if(!ArrayUtils.isEmpty(includeFields))
				{
					flag = 1;
				}
				if(!ArrayUtils.isEmpty(excludeFields))
				{
					flag = 2;
				}
				if(!ArrayUtils.isEmpty(includeFields) && !ArrayUtils.isEmpty(excludeFields))
				{
					flag = 3;
				}
				
				switch(flag){
				case 0:{
					return false;
				 }
				case 1:{
					return true;
				 }
				case 2:{
					return false;
				}
				case 3:{
					return true;
				}
				 
				}
				return false;
			}

		}).setDateFormat(datePattern);
		
		return gsonBuilder;
	}

	/*
	private GsonBuilder getIncludeGsonBuilder() {
		GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(
				new ExclusionStrategy() {
					public boolean shouldSkipClass(Class<?> skipClass) {						 
						return false;
					}
					
					public boolean shouldSkipField(FieldAttributes field) {
						if (null != includeFields && includeFields.length > 0) {
							for (String fieldName : includeFields) {
								if (field.getName().equalsIgnoreCase(fieldName))
									return false;
							}
						}
						return true;
					}
					
				}).setDateFormat(datePattern);
		
		return gsonBuilder;
		
	}*/
	
	public String toJsonString(List list) throws Exception {
		StringBuffer sb = new StringBuffer("{success:true,result:");
		sb.append(getGson().toJson(list));
		sb.append("}");
		return sb.toString();
	}
	
	public String toJsonResult(List list) {
		if (list.size() > 0) {
			try {
				return this.toJsonString(list);
			} catch (Exception e) {
				return "{success:false,message:'操作失败'}";
			}
		} else {
			return "{success:false,message:'未任何记录'}";			
		}
	}

	
	
}
